
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/comm_message_gradient_dialog.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/editable_text.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/interest_tag_entity.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/model/voice_person_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:nim_core/nim_core.dart';

import 'index_view_model.dart';
import 'my_room_view_model.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';


class RoomUpmicPersonViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;

  String yxRoomId='';
  int roomId=-1;
  List<String> defaultPersonIds = [];
  //麦位上线人员集合
  List<UserEntity> upmicPersons = [];

  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    yxRoomId =  arguments['yxRoomId'];
    roomId =  arguments['roomId'];
    defaultPersonIds =  arguments['defaultPersonIds']??[];
    LogUtil.d('RoomUpmicPersonViewModel-->yxRoomId=$yxRoomId,roomId=$roomId,defaultPersonIds=$defaultPersonIds');
    getMembers();
  }

  @override
  void onClose() {
    super.onClose();
  }

  /**
   * 获取房间成员
   */
  void getMembers() async{
    List<NIMChatroomMember> membersResult = await NIMSDKUtil.instance
        .fetchChatroomMembers(yxRoomId,1000000);
    LogUtil.d('NIMSDKUtil,getMembers isSuccess =${membersResult}');
    var membersLists = membersResult??[];
    LogUtil.d('NIMSDKUtil,getMembers membersLists =${membersLists.length}');
    if(membersLists.isNotEmpty){
      //获取服务端成员列表
      _getChatRoomServiceMembers(membersLists);

    }
  }
  //获取服务端成员列表
  void _getChatRoomServiceMembers(List<NIMChatroomMember> chatRoomMembers) async{
    List<String> yxMembersIds = (chatRoomMembers??[]).map((e) => e?.account).toList();
    if(yxMembersIds.isEmpty)return;
    Map<String,dynamic> upData={
      "roomId": roomId,
      // "yxAccids": roomTotalAccounts
      "yxAccids": yxMembersIds
    };
    BaseEntity baseEntity= await moduleIndexService
        .api_voicechat_room_queryOnlineUserInfo(parmsMap: upData);
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      upmicPersons=baseEntity?.result??[];
      upmicPersons.forEach((element) {
        if(defaultPersonIds.contains('${element?.userId}')){
          element?.selected = true;
        }
      });
      update();
    }
  }

  save() {
    var selectedPersons = (upmicPersons??[]).where((e) => (e?.selected??false)).toList();
    // if(selectedPersons.isEmpty){
    //   ToastUtil.toast(StringUtil.instance.Please_select+' '+StringUtil.instance.Onmic_Person);
    //   return;
    // }
    NavigatorUtil.goBack(result: {
      "result":'ok','selectedPersons':selectedPersons
    });
  }

  oper(int index) {
    upmicPersons[index]?.selected=!(upmicPersons[index]?.selected??false);
    update();
  }
}
